package com.open.capacity.notice.controller;

import java.util.Map;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.ImmutableMap;
import com.open.capacity.common.dto.ResponseEntity;
import com.open.capacity.notice.provider.wechat.WechatProperties;
import com.open.capacity.notice.provider.wechat.WechatTemplate;
import com.open.capacity.notice.provider.wechat.interactive.req.AcquireAddressListRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.AcquireContactInfoRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.AcquireQrCodeRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.ExecWxLoginRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.InitAddressListRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.LoginRequest;
import com.open.capacity.notice.provider.wechat.interactive.req.SecondLoginRequest;
import com.open.capacity.notice.provider.wechat.interactive.res.AcquireAddressListResponse;
import com.open.capacity.notice.provider.wechat.interactive.res.AcquireContactInfoResonse;
import com.open.capacity.notice.provider.wechat.interactive.res.AcquireQrCodeResponse;
import com.open.capacity.notice.provider.wechat.interactive.res.ExecWxLoginResponse;
import com.open.capacity.notice.provider.wechat.interactive.res.InitAddressListResponse;
import com.open.capacity.notice.provider.wechat.interactive.res.LoginResponse;
import com.open.capacity.notice.provider.wechat.interactive.res.SecondLoginResponse;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 微信接口
 */
@Slf4j
@RestController
@SuppressWarnings("all")
@RequiredArgsConstructor
@RequestMapping("/wechat")
public class MessageWechatController {

	private final WechatTemplate wechatTemplate;
	private final WechatProperties wechatProperties;
	private static final String AUTHORIZATION = "Authorization";

	/**
	 * @param request
	 * @return
	 */
	@SneakyThrows
	@PostMapping(value = "/login")
	@ApiOperation(value = "微信管理-登录", notes = "微信管理-登录")
	public ResponseEntity login(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginRequest request = BeanUtil.mapToBean(requestMap, LoginRequest.class, true);
			LoginResponse response = wechatTemplate.opsForChannel().login(request);
			return ResponseEntity.succeed(response);
		} catch (Exception e) {
			log.error("登录失败:", e);
			Assert.isTrue(false, "登录失败");
		}
		return ResponseEntity.failed("登录失败");
	}

	/**
	 * @param request
	 * @return
	 */
	@SneakyThrows
	@PostMapping(value = "/qrcode")
	@ApiOperation(value = "微信管理-获取二维码", notes = "微信管理-获取二维码")
	public ResponseEntity qrCode(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				AcquireQrCodeRequest request = BeanUtil.mapToBean(requestMap, AcquireQrCodeRequest.class, true);
				request.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				AcquireQrCodeResponse response = wechatTemplate.opsForChannel().aquireQrCode(request);
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("获取二维码失败:", e);
			Assert.isTrue(false, "获取二维码失败");
		}
		return ResponseEntity.failed("获取二维码失败");
	}

	@SneakyThrows
	@PostMapping(value = "/exec-login")
	@ApiOperation(value = "微信管理-执行登录", notes = "微信管理-执行登录")
	public ResponseEntity execLogin(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				ExecWxLoginRequest wxLoginRequest = BeanUtil.mapToBean(requestMap, ExecWxLoginRequest.class, true);
				wxLoginRequest.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				ExecWxLoginResponse response = wechatTemplate.opsForChannel().wxLogin(wxLoginRequest);
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("登录失败:", e);
			Assert.isTrue(false, "登录失败");
		}
		return ResponseEntity.failed("登录失败");
	}

	@SneakyThrows
	@PostMapping(value = "/init-address")
	@ApiOperation(value = "微信管理-初始化通讯录", notes = "微信管理-初始化通讯录")
	public ResponseEntity initAddress(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				InitAddressListRequest request = BeanUtil.mapToBean(requestMap, InitAddressListRequest.class, true);
				request.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				InitAddressListResponse response = wechatTemplate.opsForChannel().initAddressList(request);
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("初始化通讯录失败:", e);
			Assert.isTrue(false, "初始化通讯录失败");
		}
		return ResponseEntity.failed("初始化通讯录失败");
	}

	@SneakyThrows
	@PostMapping(value = "/aquire-address")
	@ApiOperation(value = "微信管理-获取通讯录", notes = "微信管理-获取通讯录")
	public ResponseEntity aquireAddress(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				AcquireAddressListRequest request = BeanUtil.mapToBean(requestMap, AcquireAddressListRequest.class,
						true);
				request.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				AcquireAddressListResponse response = wechatTemplate.opsForChannel().acquireAddressList(request);
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("获取通讯录失败:", e);
			Assert.isTrue(false, "获取通讯录失败");
		}
		return ResponseEntity.failed("获取通讯录失败");
	}

	@SneakyThrows
	@PostMapping(value = "/aquire-contact")
	@ApiOperation(value = "微信管理-获取通讯录详情", notes = "微信管理-获取通讯录详情")
	public ResponseEntity aquirecontact(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				AcquireContactInfoRequest request = BeanUtil.mapToBean(requestMap, AcquireContactInfoRequest.class,
						true);
				request.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				AcquireContactInfoResonse response = wechatTemplate.opsForChannel().acquireContactInfo(request);
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("获取通讯录详情失败:", e);
			Assert.isTrue(false, "获取通讯录详情失败");
		}
		return ResponseEntity.failed("获取通讯录详情失败");
	}
	
	
	@SneakyThrows
	@PostMapping(value = "/second-login")
	@ApiOperation(value = "微信管理-二次登录", notes = "微信管理-二次登录")
	public ResponseEntity secondLogin(@RequestBody Map<String, Object> requestMap) {
		try {
			LoginResponse loginResponse = wechatTemplate.opsForChannel().login(LoginRequest.builder()
					.account(wechatProperties.getUsername()).password(wechatProperties.getPassword()).build());
			if (loginResponse.isSuccess()) {
				SecondLoginRequest request = BeanUtil.mapToBean(requestMap, SecondLoginRequest.class,
						true);
				request.setHeaders(ImmutableMap.of(AUTHORIZATION, loginResponse.getData().getAuthorization()));
				SecondLoginResponse response = wechatTemplate.opsForChannel().secondLogin(request)   ;
				return ResponseEntity.succeed(response);
			}
		} catch (Exception e) {
			log.error("二次登录失败:", e);
			Assert.isTrue(false, "二次登录失败");
		}
		return ResponseEntity.failed("二次登录失败");
	}

}
